const Base = require('./base.js');
module.exports = class extends Base {
	async getcash(userid, source) {

		let asset = await this.where({
			user_id: userid
		}).find();
		//定时21号结算上月收入
		let monthIncome = await think.model("taoke/order").getMonthIncome(userid);

		let date = new Date();
		let nowTime = date.getTime();
		date.setDate(21);
		date.setHours(0, 0, 0, 0);
		let balanceTime = date.getTime();
		if(nowTime < balanceTime) {
			asset.amount = (asset.amount - monthIncome.this_balance_income - monthIncome.last_balance_income).toFixed(2);
		} else {
			asset.amount = (asset.amount - monthIncome.this_balance_income).toFixed(2);
		}

		//定时21号结算上月收入

		if(asset.amount < 10) {
			return {
				data: {
					amount_balance: asset.amount
				},
				code: 1
			};
		}
		if(source == 'wxmp') {

			let money = await think.model("system/money").where({
				user_id: userid,
				status: 1,
				type: 2
			}).find();
			if(think.isEmpty(money)) {
				return {
					data: {
						amount_balance: asset.amount
					},
					code: 2
				};
			}

		}

		let amount = Math.floor(asset.amount);
		let note = "提现：" + amount + " 元";
		let self = this;

		const insertId = await this.transaction(async() => {
			await self.where({
				user_id: userid
			}).decrement('amount', amount);
			const amount_balance = await self.where({
				user_id: userid
			}).getField('amount', true);
			return await think.model("system/money").db(self.db()).add({
				type: 2,
				create_time: new Date().valueOf(),
				amount: amount,
				balance: amount_balance,
				status: 0,
				change: -1,
				user_id: userid,
				note: note
			});
		});

		if(insertId) {
			const email = think.service("email");
			await email.send({
				to: think.config("setup.adminEmail"),
				title: '提现通知',
				html: `用户ID:${userid},发起提现${amount}元,请及时处理!`

			});
			return {
				data: {
					amount: amount
				},
				code: 0
			};
		} else {
			return {
				data: {},
				code: 3
			};
		}

	}

	async getbalance(userid) {
		let asset = await this.where({
			user_id: userid
		}).find();

		//定时21号结算上月收入
		let monthIncome = await think.model("taoke/order").getMonthIncome(userid);

		let date = new Date();
		let nowTime = date.getTime();
		date.setDate(21);
		date.setHours(0, 0, 0, 0);
		let balanceTime = date.getTime();
		if(nowTime < balanceTime) {
			asset.amount = (asset.amount - monthIncome.this_balance_income - monthIncome.last_balance_income).toFixed(2);
		} else {
			asset.amount = (asset.amount - monthIncome.this_balance_income).toFixed(2);
		}

		//定时21号结算上月收入

		let order_total = await think.model("taoke/order").where({
			user_id: userid
		}).count();
		let receiving_order_num = await think.model("taoke/order").where({
			user_id: userid,
			order_status: 12
		}).count();
		let receiving_withdrawals_amount = await think.model("taoke/order").where({
			user_id: userid,
			order_status: 12
		}).sum("buyer_commission");
		let doing_withdrawals_amount = await think.model("system/money").where({
			user_id: userid,
			type: 2,
			status: 0
		}).sum("amount");
		let withdrawals_total = await think.model("system/money").where({
			user_id: userid,
			type: 2,
			status: 1
		}).sum("amount");
		let user_level = await think.model("sys_user_level").where({id: asset.level}).getField("name",true);
		return {
			user_level:user_level,
			order_total: order_total || 0,
			receiving_order_num: receiving_order_num || 0,
			receiving_withdrawals_amount: receiving_withdrawals_amount || 0,
			withdrawals_amount: asset.amount,
			withdrawals_amount_limit: 10,
			doing_withdrawals_amount: doing_withdrawals_amount || 0,
			withdrawals_total: withdrawals_total || 0,
			rebate_total: asset.rebate,
			score_balance: asset.score,
			this_income: monthIncome.this_income,
			last_income: monthIncome.last_income,
			this_balance_income: monthIncome.this_balance_income,
			last_balance_income: monthIncome.last_balance_income
		};
	}

	async getInvite(userid) {
		let agency = await think.model("system/agency").where({
			user_id: userid
		}).find();
		let invite_total = await this.where({
			user_id: userid
		}).getField("team_count", true);
		let invite_rebate = await think.model("taoke/order").where({
			agency_id: agency.id
		}).sum("recommend_commission");
		let date = new Date();
		let month = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
		let monthTime = date.getFullYear() + "-" + month + "-01" + ' 00:00:00';
		let invite_month_rebate = await think.model("taoke/order").where({
			agency_id: agency.id,
			create_time: ['>', monthTime]
		}).sum("recommend_commission");
let agency_level = await think.model("sys_agency_level").where({id: agency.level}).getField("name",true);
		return {
			agency_level:agency_level,
			invite_total: invite_total,
			invite_rebate: invite_rebate || 0,
			invite_month_rebate: invite_month_rebate || 0

		};
	}

};